package geguol.cc.widgetapplication.propertyanimation;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import geguol.cc.widgetapplication.R;

/**
 * 1. 属性动画
 * 坐标：http://blog.csdn.net/dmk877/article/details/51550031
 */
public class ObjectAnimationActivity extends AppCompatActivity {


	@BindView(R.id.button7)
	Button button7;
	@BindView(R.id.animView)
	MyAnimView animView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_object_animation);
		ButterKnife.bind(this);
	}

	@BindView(R.id.move)
	Button move;

	@OnClick({R.id.move, R.id.button7})
	public void onViewClicked(View view) {
		switch (view.getId()) {
			case R.id.move:
				//		moveX();
				//		alpha();
				AnimatorSet animatorSet = new AnimatorSet();
				animatorSet.play(alpha()).with(moveX());
				animatorSet.setDuration(2000);
				animatorSet.start();
				break;
			case R.id.button7:
				startValueAnim();
				break;
		}

	}

	private void startValueAnim() {
		Point point1 = new Point(0, 0);
		Point point2 = new Point(300, 300);
		ValueAnimator anim = ValueAnimator.ofObject(new PointEvaluator(), point1, point2);
		anim.addUpdateListener(animation -> {
			Point currentPoint = (Point) animation.getAnimatedValue();
			System.out.println(currentPoint.toString());
		});
		anim.setDuration(5000);
		anim.start();
	}

	private ObjectAnimator alpha() {
		ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(move, "alpha", 1f, 0f, 1f);//每次移动100个像素
//		objectAnimator.setDuration(1500);
//		objectAnimator.start();
		objectAnimator.addListener(new AnimatorListenerAdapter() {
			@Override
			public void onAnimationStart(Animator animation) {
				super.onAnimationStart(animation);
			}
		});
		return objectAnimator;

	/*	objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
			@Override
			public void onAnimationUpdate(ValueAnimator animation) {

			}
		});*/
	}

	private ObjectAnimator moveX() {
		float tx = move.getTranslationX();//计算的是该View在X轴的偏移量。初始值为0，向左偏移值为负，向右偏移值为正
		System.out.println("getTranslationX: " + tx);
		ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(move, "translationX", tx, tx + 100);//每次移动100个像素
//		objectAnimator.setDuration(1500);//动作执行1500ms
//		objectAnimator.start();//开始移动
		return objectAnimator;
	}


}
